package com.servlet;

import com.alibaba.fastjson.JSON;
import com.dao.Impl.RechargeruleDaoImpl;
import com.dao.RechargeruleDao;
import com.entity.Rechargerule;
import com.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = "/rechargeServlet/*")
public class RechargeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String url=request.getRequestURI();
        String method=url.substring(url.lastIndexOf("/")+1);
        String target=null;
        switch (method){
            case "findRecharge":
                target=findRecharge(request,response);
                break;
            default:break;
        }
        if (target!=null){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }

    protected String findRecharge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RechargeruleDao dao=new RechargeruleDaoImpl();
        List<Rechargerule> all = dao.findAll();
        MyUtil.jsonWriter(response, all);
        return null;
    }

}
